package com.ad.pet.app.push;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;

import com.ad.pet.Logger;
import com.ad.pet.app.Navigation;
import com.ad.pet.common.structure.events.RxBus;

import org.json.JSONObject;

import cn.jpush.android.api.CustomMessage;
import cn.jpush.android.service.JPushMessageReceiver;

/**
 * 自定义接收器
 * <p>
 * 如果不定义这个 Receiver，则：
 * 1) 默认用户会打开主界面
 * 2) 接收不到自定义消息
 */
public class PushReceiver extends JPushMessageReceiver {

    @Override
    public void onMessage(Context context, CustomMessage customMessage) {

        super.onMessage(context, customMessage);
        String extra = customMessage.extra;
        Logger.e("推送--->"+extra);
        if (!TextUtils.isEmpty(extra)){
            try {
                JSONObject jsonObject = new JSONObject(extra);
                int type = 0;
                type = jsonObject.getInt("isReLogoin");
                if (type==1){
                    Navigation.goMainActivity(context);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {

                            RxBus.getDefault().post("isReLogin");
                        }
                    }, 50);

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
